import { TreeItem } from "vscode";
import { fillString } from "../utils";

export default class FundItem extends TreeItem {
  public info: FundInfo;

  constructor(props: FundInfo) {
    const changeRate: number = Number(props.changeRate);
    const icon = changeRate > 0 ? "📈" : "📉";
    const symbol = changeRate > 0 ? "+" : "-";
    const rage = `${symbol}${Math.abs(changeRate)}%`;
    const name = fillString(props.name, 25);

    super(`${icon}${name}${rage}`);

    let sliceName: string = props.name;

    if (sliceName.length > 8) {
      sliceName = `${sliceName.slice(0, 8)}...`;
    }

    const tip = [
      `基金代码:    ${props.code}`,
      `基金名称:    ${sliceName}`,
      "---------------------------",
      `单位净值:    ${props.now}`,
      `涨跌幅:      ${props.changeRate}%`,
      `涨跌额:      ${props.changeAmount}`,
      `昨收:        ${props.lastClose}`,
    ];

    this.info = props;
    this.tooltip = tip.join("\r\n");
  }
}
